home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GeometryByReference / InterleavedNIOBuffer.java.z / InterleavedNIOBuffer.java
Encoding:
Java Source  |  2003-08-08  |  15.7 KB  |  527 lines

  1. /*
  2.  *    @(#)InterleavedNIOBuffer.java 1.4 02/10/21 13:42:27
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import com.sun.j3d.utils.applet.MainFrame;
  44. import com.sun.j3d.utils.geometry.*;
  45. import com.sun.j3d.utils.universe.*;
  46. import com.sun.j3d.utils.image.TextureLoader;
  47. import javax.media.j3d.*;
  48. import javax.vecmath.*;
  49. import javax.swing.*;
  50. import javax.swing.event.*;
  51. import javax.swing.border.*;
  52. import com.sun.j3d.utils.behaviors.vp.*;
  53. import java.nio.*;
  54.  
  55. public class InterleavedNIOBuffer extends JApplet implements ActionListener {
  56.  
  57.     RenderingAttributes ra;
  58.     ColoringAttributes ca;
  59.     Material mat;               
  60.     Appearance app;               
  61.     JComboBox geomType;
  62.     JCheckBox transparency;
  63.     JCheckBox textureBox;
  64.     Shape3D shape;               
  65.     TransparencyAttributes transp;
  66.  
  67.     GeometryArray tetraRegular, tetraStrip, tetraIndexed, tetraIndexedStrip;
  68.     GeometryArray[] geoArrays = new GeometryArray[4];
  69.  
  70.    // Globally used colors
  71.    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
  72.    Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
  73.    Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
  74.    Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
  75.    Color3f[] colors = {white, red, green, blue};               
  76.  
  77.     private static final float sqrt3 = (float) Math.sqrt(3.0);
  78.     private static final float sqrt3_3 = sqrt3 / 3.0f;
  79.     private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
  80.  
  81.     private static final float ycenter = 0.5f * sqrt24_3;
  82.     private static final float zcenter = -sqrt3_3;
  83.  
  84.     private static final Point3f p1 = 
  85.     new Point3f(-1.0f, -ycenter, -zcenter);
  86.     private static final Point3f p2 = 
  87.     new Point3f(1.0f, -ycenter, -zcenter);
  88.     private static final Point3f p3 =
  89.     new Point3f(0.0f, -ycenter, -sqrt3 - zcenter);
  90.     private static final Point3f p4 =
  91.     new Point3f(0.0f, sqrt24_3 - ycenter, 0.0f);
  92.  
  93.     private static final Point2f t1 = new Point2f(0.0f, 0.0f);
  94.     private static final Point2f t2 = new Point2f(0.5f, 1.0f);
  95.     private static final Point2f t3 = new Point2f(1.0f, 0.0f);
  96.     private static final Point2f t4 = new Point2f(1.0f, 1.0f);
  97.  
  98.     private static final Color3f c1 = new Color3f(1.0f, 0.0f, 0.0f);
  99.     private static final Color3f c2 = new Color3f(0.0f, 1.0f, 0.0f);
  100.     private static final Color3f c3 = new Color3f(0.0f, 1.0f, 1.0f);
  101.     private static final Color3f c4 = new Color3f(1.0f, 1.0f, 0.0f);
  102.     
  103.  
  104.     private static final float[] interleaved = {
  105.     t1.x, t1.y,
  106.     t1.x, t1.y,
  107.     c1.x, c1.y, c1.z, // front face
  108.     p1.x, p1.y, p1.z, // front face
  109.     t2.x, t2.y,
  110.     t2.x, t2.y,
  111.     c2.x, c2.y, c2.z,
  112.     p2.x, p2.y, p2.z,
  113.     t4.x, t4.y,
  114.     t4.x, t4.y,
  115.     c4.x, c4.y, c4.z,
  116.     p4.x, p4.y, p4.z,
  117.     
  118.     t1.x, t1.y,
  119.     t1.x, t1.y,
  120.     c1.x, c1.y, c1.z,// left, back face
  121.     p1.x, p1.y, p1.z,// left, back face
  122.     t4.x, t4.y,
  123.     t4.x, t4.y,
  124.     c4.x, c4.y, c4.z,
  125.     p4.x, p4.y, p4.z,
  126.     t3.x, t3.y,
  127.     t3.x, t3.y,
  128.     c3.x, c3.y, c3.z,
  129.     p3.x, p3.y, p3.z,
  130.     
  131.     t2.x, t2.y,
  132.     t2.x, t2.y,
  133.     c2.x, c2.y, c2.z,// right, back face
  134.     p2.x, p2.y, p2.z,// right, back face
  135.     t3.x, t3.y,
  136.     t3.x, t3.y,
  137.     c3.x, c3.y, c3.z,
  138.     p3.x, p3.y, p3.z,
  139.     t4.x, t4.y,
  140.     t4.x, t4.y,
  141.     c4.x, c4.y, c4.z,
  142.     p4.x, p4.y, p4.z,
  143.     
  144.     t1.x, t1.y,
  145.     t1.x, t1.y,
  146.     c1.x, c1.y, c1.z,// bottom face
  147.     p1.x, p1.y, p1.z,// bottom face
  148.     t3.x, t3.y,
  149.     t3.x, t3.y,
  150.     c3.x, c3.y, c3.z,
  151.     p3.x, p3.y, p3.z,
  152.     t2.x, t2.y,
  153.     t2.x, t2.y,
  154.     c2.x, c2.y, c2.z,
  155.     p2.x, p2.y, p2.z,
  156.     };
  157.  
  158.     private static final float[] indexedInterleaved = {
  159.     t1.x,t1.y,
  160.     t1.x,t1.y,
  161.     c1.x,c1.y,c1.z,
  162.     p1.x,p1.y,p1.z,
  163.     t2.x,t2.y,
  164.     t2.x,t2.y,
  165.     c2.x,c2.y,c2.z,
  166.     p2.x,p2.y,p2.z,
  167.     t3.x,t3.y,
  168.     t3.x,t3.y,
  169.     c3.x,c3.y,c3.z,
  170.     p3.x,p3.y,p3.z,
  171.     t4.x,t4.y,
  172.     t4.x,t4.y,
  173.     c4.x,c4.y,c4.z,
  174.     p4.x,p4.y,p4.z,
  175.     };
  176.     
  177.  
  178.     private static final int[] indices = {0,1,3,0,3,2,1,2,3,0,2,1};
  179.     private int[] stripVertexCounts = {3,3,3,3};
  180.  
  181.     TextureUnitState textureUnitState[] = new TextureUnitState[2];
  182.     Texture tex1;
  183.     Texture tex2;
  184.  
  185.     private java.net.URL texImage1 = null;
  186.     private java.net.URL texImage2 = null;
  187.  
  188.     private SimpleUniverse u;
  189.  
  190.     private J3DBuffer interleavedBuffer;
  191.     private J3DBuffer indexedInterleavedBuffer;
  192.  
  193.     void createInterleavedBuffers() {
  194.     int size;
  195.     ByteOrder order =  ByteOrder.nativeOrder();
  196.  
  197.     size =  (2 + 2 + 3 + 3 ) * 3 * 4; 
  198.     FloatBuffer vertex = ByteBuffer.allocateDirect(size * 4).order(order).asFloatBuffer();
  199.     vertex.put(interleaved, 0, size);
  200.     interleavedBuffer = new J3DBuffer(vertex);
  201.  
  202.     size = ( 2 + 2 + 3 + 3) * 4;
  203.     FloatBuffer indexedVertex = ByteBuffer.allocateDirect(size * 4).order(order).asFloatBuffer();
  204.     indexedVertex.put(indexedInterleaved, 0, size);
  205.     indexedInterleavedBuffer = new J3DBuffer(indexedVertex);
  206.     }
  207.     
  208.     BranchGroup createSceneGraph() {
  209.     BranchGroup objRoot = new BranchGroup();
  210.  
  211.     // Set up attributes to render lines
  212.         app = new Appearance();
  213.     app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);
  214.     
  215.     transp = new TransparencyAttributes();
  216.     transp.setTransparency(0.5f);
  217.     transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
  218.     transp.setTransparencyMode(TransparencyAttributes.NONE);
  219.     app.setTransparencyAttributes(transp);
  220.  
  221.         // load textures
  222.         TextureAttributes texAttr1 = new TextureAttributes();
  223.         texAttr1.setTextureMode(TextureAttributes.DECAL);
  224.         TextureAttributes texAttr2 = new TextureAttributes();
  225.         texAttr2.setTextureMode(TextureAttributes.MODULATE);
  226.  
  227.         TextureLoader tex = new TextureLoader(texImage1, new String("RGB"), this);
  228.         if (tex == null)
  229.             return null;
  230.         tex1 = tex.getTexture();
  231.  
  232.         tex = new TextureLoader(texImage2, new String("RGB"), this);
  233.         if (tex == null)
  234.             return null;
  235.         tex2 = tex.getTexture();
  236.  
  237.         textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null);
  238.         textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null);
  239.  
  240.     createInterleavedBuffers();
  241.     
  242.     tetraRegular = createGeometry(1);
  243.     tetraStrip =createGeometry(2);
  244.     tetraIndexed = createGeometry(3);
  245.     tetraIndexedStrip = createGeometry(4);
  246.  
  247.     geoArrays[0] = tetraRegular;
  248.     geoArrays[1] = tetraStrip;
  249.     geoArrays[2] = tetraIndexed;
  250.     geoArrays[3] = tetraIndexedStrip;
  251.     
  252.     shape = new Shape3D(tetraRegular, app);
  253.     shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
  254.  
  255.     Transform3D t = new Transform3D();
  256.     // move the object upwards
  257.     t.set(new Vector3f(0.0f, 0.3f, 0.0f));
  258.  
  259.     // rotate the shape
  260.     Transform3D temp = new Transform3D();
  261.         temp.rotX(Math.PI/4.0d);
  262.     t.mul(temp);
  263.         temp.rotY(Math.PI/4.0d);
  264.         t.mul(temp);
  265.     
  266.     // Shrink the object 
  267.     t.setScale(0.6);
  268.  
  269.     TransformGroup trans = new TransformGroup(t);
  270.     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  271.     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  272.  
  273.     objRoot.addChild(trans);
  274.     trans.addChild(shape);
  275.  
  276.     BoundingSphere bounds =
  277.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  278.  
  279.     // Set up the global lights
  280.     Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
  281.     Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
  282.     Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
  283.  
  284.     AmbientLight aLgt = new AmbientLight(alColor);
  285.     aLgt.setInfluencingBounds(bounds);
  286.     DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
  287.     lgt1.setInfluencingBounds(bounds);
  288.     objRoot.addChild(aLgt);
  289.     objRoot.addChild(lgt1);
  290.     
  291.     // Let Java 3D perform optimizations on this scene graph.
  292.         objRoot.compile();
  293.  
  294.     return objRoot;
  295.     }
  296.  
  297.     JPanel createGeometryByReferencePanel() {
  298.     JPanel panel = new JPanel();
  299.     panel.setBorder(new TitledBorder("Geometry Type"));
  300.  
  301.     String values[] = {"Array", "Strip", "Indexed", "IndexedStrip"};
  302.     geomType = new JComboBox(values);
  303.     geomType.setLightWeightPopupEnabled(false);
  304.     geomType.addActionListener(this);
  305.     geomType.setSelectedIndex(0);
  306.     panel.add(new JLabel("Geometry Type"));     
  307.     panel.add(geomType);
  308.  
  309.     transparency = new JCheckBox("EnableTransparency", 
  310.                       false);
  311.     transparency.addActionListener(this);
  312.     panel.add(transparency);
  313.  
  314.     textureBox = new JCheckBox("EnableTexture", false);
  315.     textureBox.addActionListener(this);
  316.     panel.add(textureBox);
  317.  
  318.     return panel;
  319.     }
  320.  
  321.     public InterleavedNIOBuffer() {
  322.     }
  323.  
  324.     public InterleavedNIOBuffer(java.net.URL texURL1, java.net.URL texURL2) {
  325.     texImage1 = texURL1;
  326.     texImage2 = texURL2;
  327.     }
  328.  
  329.     public void init() {
  330.  
  331.     // create textures
  332.  
  333.         if (texImage1 == null) {
  334.           // the path to the image for an applet
  335.           try {
  336.             texImage1 = new java.net.URL(getCodeBase().toString() +
  337.                                       "../images/bg.jpg");
  338.           }
  339.           catch (java.net.MalformedURLException ex) {
  340.             System.out.println(ex.getMessage());
  341.             System.exit(1);
  342.           }
  343.         }
  344.  
  345.         if (texImage2 == null) {
  346.           // the path to the image for an applet
  347.           try {
  348.             texImage2 = new java.net.URL(getCodeBase().toString() +
  349.                                     "../images/one.jpg");
  350.           }
  351.           catch (java.net.MalformedURLException ex) {
  352.             System.out.println(ex.getMessage());
  353.             System.exit(1);
  354.           }
  355.         }
  356.  
  357.     Container contentPane = getContentPane();
  358.     
  359.         Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  360.         contentPane.add("Center", c);
  361.  
  362.         BranchGroup scene = createSceneGraph();
  363.         // SimpleUniverse is a Convenience Utility class
  364.         u = new SimpleUniverse(c);
  365.  
  366.     // add mouse behaviors to the viewingPlatform
  367.     ViewingPlatform viewingPlatform = u.getViewingPlatform();
  368.  
  369.         // This will move the ViewPlatform back a bit so the
  370.         // objects in the scene can be viewed.
  371.         viewingPlatform.setNominalViewingTransform();
  372.  
  373.     // add Orbit behavior to the viewing platform
  374.     OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
  375.     BoundingSphere bounds =
  376.         new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  377.     orbit.setSchedulingBounds(bounds);
  378.     viewingPlatform.setViewPlatformBehavior(orbit);
  379.     
  380.         u.addBranchGraph(scene);
  381.  
  382.  
  383.     // Create GUI
  384.     JPanel p = new JPanel();
  385.     BoxLayout boxlayout = new BoxLayout(p, 
  386.                         BoxLayout.Y_AXIS);
  387.     p.add(createGeometryByReferencePanel());
  388.     p.setLayout(boxlayout);
  389.  
  390.     contentPane.add("South", p);
  391.     }
  392.  
  393.     public void destroy() {
  394.     u.cleanup();
  395.     }
  396.  
  397.     public void actionPerformed(ActionEvent e) {
  398.     Object target = e.getSource();
  399.     if (target == geomType) {
  400.         shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);
  401.  
  402.     } 
  403.     else if (target == transparency) {
  404.         if (transparency.isSelected()) {
  405.         transp.setTransparencyMode(TransparencyAttributes.BLENDED);
  406.         }
  407.         else {
  408.         transp.setTransparencyMode(TransparencyAttributes.NONE);
  409.         }
  410.     } 
  411.     else if (target == textureBox) {
  412.         if (textureBox.isSelected()) {
  413.         app.setTextureUnitState(textureUnitState);
  414.         }
  415.         else {
  416.         app.setTextureUnitState(null);
  417.         }
  418.     }
  419.     }
  420.  
  421.  
  422.                
  423.     public static void main(String[] args) {
  424.         java.net.URL texURL1 = null;
  425.         java.net.URL texURL2 = null;
  426.         // the path to the image for an application
  427.         try {
  428.           texURL1 = new java.net.URL("file:../images/bg.jpg");
  429.           texURL2 = new java.net.URL("file:../images/one.jpg");
  430.         }
  431.         catch (java.net.MalformedURLException ex) {
  432.           System.out.println(ex.getMessage());
  433.           System.exit(1);
  434.         }
  435.  
  436.     Frame frame = new MainFrame(new InterleavedNIOBuffer(texURL1, texURL2), 
  437.                     800, 800);
  438.     }
  439.                
  440.     public GeometryArray createGeometry (int type) {
  441.     GeometryArray tetra = null;
  442.         int texCoordSetMap[] = {0, 0};
  443.  
  444.     if (type == 1) {
  445.         tetra =new TriangleArray(12, 
  446.                      TriangleArray.COORDINATES|
  447.                      TriangleArray.COLOR_3|
  448.                      /*
  449.                        TriangleArray.NORMAL_3|
  450.                        */
  451.                      TriangleArray.TEXTURE_COORDINATE_2 |
  452.                      TriangleArray.INTERLEAVED|
  453.                      TriangleArray.BY_REFERENCE|
  454.                      TriangleArray.USE_NIO_BUFFER,
  455.                      2, texCoordSetMap);
  456.         
  457.         tetra.setInterleavedVertexBuffer(interleavedBuffer);
  458.  
  459.     }
  460.     else if (type == 2) {
  461.         tetra = new TriangleStripArray(12,
  462.                        TriangleStripArray.COORDINATES|
  463.                        TriangleStripArray.COLOR_3|
  464.                        /*
  465.                          TriangleArray.NORMAL_3|
  466.                          */
  467.                        TriangleArray.TEXTURE_COORDINATE_2 |
  468.                        TriangleStripArray.INTERLEAVED|
  469.                        TriangleStripArray.BY_REFERENCE|
  470.                        TriangleStripArray.USE_NIO_BUFFER,
  471.                        2, texCoordSetMap,
  472.                        stripVertexCounts);
  473.         tetra.setInterleavedVertexBuffer(interleavedBuffer);
  474.  
  475.         
  476.     }
  477.     else if (type == 3) { // Indexed Geometry
  478.         tetra = new IndexedTriangleArray(4,
  479.                          IndexedTriangleArray.COORDINATES|
  480.                          IndexedTriangleArray.COLOR_3|
  481.                          /*
  482.                            IndexedTriangleArray.NORMAL_3|
  483.                            */
  484.                          IndexedTriangleArray.TEXTURE_COORDINATE_2 |
  485.                          IndexedTriangleArray.INTERLEAVED|
  486.                          IndexedTriangleArray.BY_REFERENCE|
  487.                          IndexedTriangleArray.USE_NIO_BUFFER|
  488.                          IndexedTriangleArray.USE_COORD_INDEX_ONLY,
  489.                          2, texCoordSetMap,
  490.                          12);
  491.         tetra.setInterleavedVertexBuffer(indexedInterleavedBuffer);
  492.         ((IndexedTriangleArray)tetra).setCoordinateIndices(0, indices);
  493.         ((IndexedTriangleArray)tetra).setColorIndices(0, indices);
  494.         ((IndexedTriangleArray)tetra).setTextureCoordinateIndices(
  495.                     0, 0, indices);
  496.         ((IndexedTriangleArray)tetra).setTextureCoordinateIndices(
  497.                     1, 0, indices);
  498.     }
  499.     else if (type == 4) { // Indexed strip geometry
  500.         tetra = new IndexedTriangleStripArray(4,
  501.                           IndexedTriangleStripArray.COORDINATES|
  502.                           IndexedTriangleStripArray.COLOR_3|
  503.                           /*
  504.                             IndexedTriangleArray.NORMAL_3|
  505.                             */
  506.                           IndexedTriangleArray.TEXTURE_COORDINATE_2 |
  507.                           IndexedTriangleStripArray.INTERLEAVED|
  508.                           IndexedTriangleStripArray.BY_REFERENCE|
  509.                           IndexedTriangleStripArray.USE_NIO_BUFFER,
  510.                           //IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
  511.                               2, texCoordSetMap,
  512.                           12,
  513.                           stripVertexCounts);
  514.         tetra.setInterleavedVertexBuffer(indexedInterleavedBuffer);
  515.         ((IndexedTriangleStripArray)tetra).setCoordinateIndices(0, indices);
  516.         ((IndexedTriangleStripArray)tetra).setColorIndices(0, indices);
  517.         ((IndexedTriangleStripArray)tetra).setTextureCoordinateIndices(
  518.                     0, 0, indices);
  519.         ((IndexedTriangleStripArray)tetra).setTextureCoordinateIndices(
  520.                     1, 0, indices);
  521.     }
  522.     else if (type == 5) { // Interleaved array
  523.     }
  524.     return tetra;
  525.     }
  526. }
  527.